/**
 * ...
 * @author Pavel Kogan
 */
package com.ADK2 {
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.LoaderContext;
	import flash.system.ApplicationDomain;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	import flash.net.URLRequest;
	import com.ADK2.Events.ADK2Events;
	public class ADK2_AD extends Sprite 
	{
		private var adLoder:Loader=new Loader();
		private var ADK2Ad_Url:String = "http://static.adk2.com/players/adk2_player/adk2_player_loader.swf?clientid=";
		private var w:int=0;
		private var h:int = 0;
		private var square:Sprite = new Sprite();
		public var bgCollor = 0xFFFFFF;
		public var bgAlpha:Number = 0.5;
		public function ADK2_AD() 
		{
			
		}
		public function initADK2_ad(id:String) {
			
			
			this.addChild(square);
			square.graphics.beginFill(bgCollor);
			square.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			square.graphics.endFill();
			square.alpha = bgAlpha;
			square.addEventListener(MouseEvent.CLICK, clickStoper);
			//square.x = stage.stageWidth/2-square.width/2;
			//square.y = stage.stageHeight/2-square.height/2;
			Security.allowDomain("*")
			ADK2Ad_Url += id +"&w=" + stage.stageWidth + "&h=" + stage.stageHeight;
			try {
				var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain,SecurityDomain.currentDomain);
				adLoder.load(new URLRequest(ADK2Ad_Url),context);
			} catch (e:Error) {
				adLoder.load(new URLRequest(ADK2Ad_Url));
			}
			adLoder.contentLoaderInfo.addEventListener(Event.COMPLETE,playerLoaded);
			
			//addChild(clipLoder);
			
		}
		private function clickStoper(eve:MouseEvent):void
		{
		}
		private function playerLoaded(eve:Event):void
		{
			adLoder.content.addEventListener("playbackEnd", onAdk2playbackEnd);
			this.addChild(adLoder);
		}
		private function onAdk2playbackEnd(eve:Event):void
		{
			trace("playbackEnd"+ adLoder.parent);
			adLoder.removeEventListener("playbackEnd", onAdk2playbackEnd);
			//this.removeChildAt(1);
			adLoder.unload();
			//this.removeChildAt(1);
			square.removeEventListener(MouseEvent.CLICK, clickStoper);
			this.removeChildAt(0);
			dispatchEvent(new ADK2Events(ADK2Events.PLAYBACKEND, null, true));
			
		}
		
		//ad_vast_example
	}
}